﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/TwoCol.Master" AutoEventWireup="True" CodeBehind="Index.aspx.cs" Inherits="P.InOut.NetMvc.Views.Updates.Index" %>
<%@ Import Namespace="P.InOut.NetMvc.Controllers" %>

<asp:Content ID="Scripts" ContentPlaceHolderID="Scripts" runat="server">
<!-- // Place any necessary javascript here -->
<script src="Scripts/jquery.js" type="text/javascript"></script>
<script src="Scripts/jquery.maskedinput.js" type="text/javascript"></script>
<script type="text/javascript">

    // variable used to detect if form being edited
    var editing = false;
    $(document).ready(function($) {

        $("#inStatus").bind("change", function() {
            $("#inStatusForm").submit();
        });

        $("#officeStatus").bind("change", function() {
            $("#inStatusForm").submit();
        });

        $("#InsertUpdateTextBox").bind("focus", function() {
            editing = true;
            setTimeout(function() {
                editing = false;
            }, 15000);
        });

        $("#InsertUpdateTextBox").bind("blur", function() {
            editing = false;
        });

        $("#StatusText").bind("focus", function() {
            editing = true;
            setTimeout(function() {
                editing = false;
            }, 15000);
        });

        $("#StatusText").bind("blur", function() {
            editing = false;
        });

        setInterval(function() {
            if (!editing) {
                var updateText = $("#InsertUpdateTextBox").val();

                if (updateText.length > 0)
                    createCookie("updateText", updateText, 1);

                window.location.reload();
            }
        }, 60000);

        var updateText = readCookie("updateText");
        if (updateText) {
            $("#InsertUpdateTextBox").val(updateText);
            eraseCookie("updateText");
        }
    });

    $(document).unload(function($) {
        $("#inStatus").unbind();
        $("#officeStatus").unbind();
        $("#InsertUpdateTextBox").unbind();
        $("#StatusText").unbind();
    });

    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toUTCString();
        } else {
            var expires = "";
        }

        document.cookie = name + "=" + value + expires + "; path=/";
    }

    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(length, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }

    function eraseCookie(name) {
        createCookie(name, "", -1);
    }

    function editUpdate(id) {

        $("#" + id.toString()).css("display", "none");
        $("#editLink" + id.toString()).css("display", "none");
        $("#elapsed" + id.toString()).css("display", "none");
        $("#edit" + id.toString()).css("display", "inline");
        editing = true;
    }

    function cancelEdit(id) {

        $("#edit" + id.toString()).css("display", "none");
        $("#" + id.toString()).css("display", "inline");
        $("#editLink" + id.toString()).css("display", "inline");
        $("#elapsed" + id.toString()).css("display", "inline");
        editing = false;
    }

    function getTinyUrl() {
        var url = prompt("Input URL (do not input http://)", "");
        url = "Utilities/getTinyUrl?url=" + url;

        var hasHttp = url.match("http://");

        if (!hasHttp) {
            if (url != null) {
                $.ajax({
                    async: false,
                    cache: false,
                    type: "GET",
                    url: url,
                    success: function(response) {
                        insertTinyUrl(response);
                    }
                });
            }
        } else {
            getTinyUrl();
        }
    }

    function insertTinyUrl(url) {
        editing = true;
        var val = $("#InsertUpdateTextBox").val();

        url = url.replace(/\"/g, "");
        if (url.match("http://"))
            $("#InsertUpdateTextBox").val(val + url);
        else 
            $("#tinyError").val("There was an error communicating to tinyurl's api");
    }
</script>
</asp:Content>

<asp:Content ID="Header" ContentPlaceHolderID="Header" runat="server">
<!-- // Place Header 100% Width Content Here -->
</asp:Content>

<asp:Content ID="Col1" ContentPlaceHolderID="Col1" runat="server">
<!-- // Place all LeftColumn Content Here -->
    <div id="statusform">
        <%= Html.RenderComponent<ProfileComponentController>(c => c.Status()) %>
    </div>
    <div id="updateform">
        <span style="font-weight: bolder;">Add an entry</span>
        <span id="tinyError" class="error" style="color: Red; font-weight: bolder;"></span>
        <% using (Html.Form<UpdatesController>(action => action.Create()))
           { %>
            
            <%= Html.TextBox("UpdateText", new { id = "InsertUpdateTextBox" })%>
        <% } %>
        <p>save space, use a <a href="javascript:getTinyUrl()">tinyurl</a></p>
    </div>
    <!-- // Wrap the Updates Component in a Div for easy CSS -->
    <div id="updates">
        <%= Html.RenderComponent<UpdateComponentController>(c => c.Updates()) %>
    </div>
</asp:Content>

<asp:Content ID="Col2" ContentPlaceHolderID="Col2" runat="server">
<!-- // Place all RightColumn Content Here -->

    <!-- // Wrap the TeamUpdates Component in a Div for easy CSS -->
    <div id="memberUpdates">
        <%= Html.RenderComponent<UpdateComponentController>(c => c.TeamUpdates()) %>
    </div>
</asp:Content>
